package dbinit.csv;

import java.io.FileNotFoundException;
import java.io.IOException;


import core.product.model.Product;
import core.product.model.ProductSearchKey;

import dbinit.InitProduct;
import dbinit.InitProductSearchKey;
import dbinit.csv.csvreader.CsvReader;

public class ImportProductSearch 
{
	public static void doImport(String Filename)
	{
		String filepath="source/";

		try {
			CsvReader productdata = new CsvReader(filepath+Filename);
			productdata.readHeaders();
			while (productdata.readRecord())
			{
				String sku=productdata.get("sku");
				String searchword=productdata.get("searchword");
				if(sku != null && sku.trim()!= "")
				{
					sku = sku.trim();
				}
				if(searchword != null && searchword.trim()!= "")
				{
					searchword = searchword.trim();
				}
				Long pId = InitProduct.getProductIdBySku(sku);
				if(pId != null)
				{
					Product p = InitProduct.getProductByUuid(pId);
					if(p != null)
					{
						Long searchUuid = InitProductSearchKey.doinit(searchword);
						ProductSearchKey psk = InitProductSearchKey.getProductSearchKeyByUuid(searchUuid);
						InitProduct.updateProductSearchKey(p,psk);
					}
					else
					{
						System.out.println("Product not found with sku :- " + sku );
					}
				}
			}
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		doImport("product_searchKeyword.csv");
	}

}
